package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @Author: WangHongKun
 * @Date: 2022/9/22 16:58
 * @Email: 2028911483@qq.com
 * @Phone: 18683977706
 */

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper  dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional
    public BusinessDataVO getTodayBusinessData() {
        LocalDate today = LocalDateTime.now().toLocalDate();
        LocalDateTime begin = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(today, LocalTime.MAX);
        Integer newUser = userMapper.getNewUser(begin, end);
        Double turnover = orderMapper.getSumByStatusAndTime(Orders.COMPLETED, begin, end);
        if(turnover ==null)
        {
            turnover =0.0;
        }
        Integer validDayOrders = orderMapper.getEveryValidDayOrders(Orders.COMPLETED, begin, end);
        Integer dayOrders = orderMapper.getEveryDayOrders(begin, end);
        Double unitPrice =0.0;
        Double orderCompletionRate =0.0;
        if(validDayOrders !=0){
             unitPrice = turnover/validDayOrders;
        }
        if(dayOrders !=0){
             orderCompletionRate = (double)validDayOrders/dayOrders;
        }
        return new BusinessDataVO(turnover,validDayOrders,orderCompletionRate,unitPrice,newUser);
    }

    @Override
    public OrderOverViewVO getOverViewOrders() {
        Integer allOrders = orderMapper.getEveryDayOrders(null, null);
        Integer allCancelled = orderMapper.getEveryValidDayOrders(Orders.CANCELLED, null, null);
        Integer allCompleted = orderMapper.getEveryValidDayOrders(Orders.COMPLETED, null, null);
        Integer allToConfirmed= orderMapper.getEveryValidDayOrders(Orders.TO_BE_CONFIRMED, null, null);
        Integer allConfirmed= orderMapper.getEveryValidDayOrders(Orders.CONFIRMED, null, null);
        return new OrderOverViewVO(allToConfirmed,allConfirmed,allCompleted,allCancelled,allOrders);
    }

    @Override
    public DishOverViewVO getOverviewDishes() {
        Integer sold = dishMapper.getCountByStatus(1);//启售数量
        Integer discontinued = dishMapper.getCountByStatus(0);
        return new DishOverViewVO(sold,discontinued);
    }

    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        Integer sold = setmealMapper.getCountByStatus(1);
        Integer discontinued = setmealMapper.getCountByStatus(0);
        return new SetmealOverViewVO(sold,discontinued);
    }

    @Override
    public BusinessDataVO getTodayBusinessData1(LocalDateTime begin, LocalDateTime end) {
        Integer newUser = userMapper.getNewUser(begin, end);
        Double turnover = orderMapper.getSumByStatusAndTime(Orders.COMPLETED, begin, end);
        if(turnover ==null)
        {
            turnover =0.0;
        }
        Integer validDayOrders = orderMapper.getEveryValidDayOrders(Orders.COMPLETED, begin, end);
        Integer dayOrders = orderMapper.getEveryDayOrders(begin, end);
        Double unitPrice =0.0;
        Double orderCompletionRate =0.0;
        if(validDayOrders !=0){
            unitPrice = turnover/validDayOrders;
        }
        if(dayOrders !=0){
            orderCompletionRate = (double)validDayOrders/dayOrders;
        }
        return new BusinessDataVO(turnover,validDayOrders,orderCompletionRate,unitPrice,newUser);
    }
}
